<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">final关键字与权限修饰符</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-09-24</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h2 id="一、final关键字"><a href="#一、final关键字" class="headerlink" title="一、final关键字"></a>一、final关键字</h2><p>final关键字代表最终、不可改变的的。</p>
<p>常见四种用法：</p>
<ol>
<li>可以用来修饰一个类</li>
<li>还可以修饰一个方法</li>
<li>还可以用来修饰一个局部变量</li>
<li>还可以用来修饰一个成员变量</li>
</ol>
<h4 id="修饰类"><a href="#修饰类" class="headerlink" title="修饰类"></a>修饰类</h4><p>当final关键字用来修饰一个类的时候，</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> 类名称 </span>&#123;</span><br><span class="line"><span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>含义：当前这个类，不能有任何子类，也就是太监类（继承就会报错）</p>
<p>注意：一个类如果是final的，那么其中的所有的成员方法都不能进行覆盖重写（因为没有儿子）</p>
<h4 id="修饰方法"><a href="#修饰方法" class="headerlink" title="修饰方法"></a>修饰方法</h4><p>当final关键字用来修饰一个方法的时候，这个方法就是最终方法，也就是不能被覆盖重写。</p>
<p>格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">修饰符 <span class="keyword">final</span> 返回值类型 方法列表(参数列表)&#123;</span><br><span class="line"><span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意事项：</p>
<p>对于类、方法来说，abstract和final关键字不能同时使用，因为矛盾(abstract是一定要被重写的，final关键字是不能被重写的)</p>
<h4 id="修饰局部变量"><a href="#修饰局部变量" class="headerlink" title="修饰局部变量"></a>修饰局部变量</h4><p>Student.java</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> &#123;</span></span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight processing"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(<span class="keyword">String</span>[] args) &#123;</span><br><span class="line">		<span class="built_in">int</span> num=<span class="number">10</span>;</span><br><span class="line">		System.out.<span class="built_in">println</span>(num);<span class="comment">//10</span></span><br><span class="line">		num=<span class="number">20</span>;</span><br><span class="line">		System.out.<span class="built_in">println</span>(num);<span class="comment">//20</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//一旦使用final来修饰局部变量，那么这个变量就不能进行更改</span></span><br><span class="line">		<span class="comment">//一次赋值，终身不变</span></span><br><span class="line">		<span class="keyword">final</span> <span class="built_in">int</span> num1=<span class="number">10</span>;</span><br><span class="line">		System.out.<span class="built_in">println</span>(num1);<span class="comment">//10</span></span><br><span class="line">		<span class="comment">//num1=20;//错误写法，不能改变</span></span><br><span class="line">		<span class="comment">//num1=10;//即使数值一样也是错误写法，只能赋值一次</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">final</span> <span class="built_in">int</span> num2;</span><br><span class="line">		num2=<span class="number">10</span>;<span class="comment">//这个是正确写法，只要保证赋值一次即可</span></span><br><span class="line">		System.out.<span class="built_in">println</span>(num2);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//对于基本类型来说，不可变，指的是变量当中的值不可改变</span></span><br><span class="line">		<span class="comment">//对于引用类型来说，不可变，指的是变量当中的地址值不可改变，但是存储的值是可以变的</span></span><br><span class="line">		Student stu1 = <span class="keyword">new</span> Student(<span class="string">"哈哈"</span>);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu1);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu1.getName());<span class="comment">//哈哈</span></span><br><span class="line">		stu1=<span class="keyword">new</span> Student(<span class="string">"呵呵"</span>);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu1);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu1.getName());<span class="comment">//呵呵</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">final</span> Student stu2=<span class="keyword">new</span> Student(<span class="string">"娃哈哈"</span>);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu2+stu2.getName());<span class="comment">//地址不变+娃哈哈</span></span><br><span class="line"><span class="comment">//		stu2=new Student("哦呵呵");//报错</span></span><br><span class="line"><span class="comment">//		System.out.println(stu2+stu2.getName());</span></span><br><span class="line">		</span><br><span class="line">		stu2.setName(<span class="string">"哦呵呵"</span>);</span><br><span class="line">		System.out.<span class="built_in">println</span>(stu2+stu2.getName());<span class="comment">//地址不变+哦呵呵</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h4 id="修饰成员变量"><a href="#修饰成员变量" class="headerlink" title="修饰成员变量"></a>修饰成员变量</h4><p>对于成员变量来说，如果使用final关键字来修饰，那么这个变量也不可变</p>
<ol>
<li>由于成员变量具有默认值，所以用了final之后，必须手动赋值，不然就是默认值，且无法改变</li>
<li>对于final的成员变量，要不直接赋值，要不通过构造方法赋值。二者选其一</li>
<li>必须保证类当中所有重载的构造方法，都最终会对final的成员变量进行赋值，如果不能用，就直接赋值</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">	<span class="comment">//方案一</span></span><br><span class="line">	<span class="comment">//private final String name="八戒";</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//方案二</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">final</span> String name;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name=<span class="string">"八戒"</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Person</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、权限修饰符"><a href="#二、权限修饰符" class="headerlink" title="二、权限修饰符"></a>二、权限修饰符</h2><p>Java中有四种权限修饰符</p>
<table>
<thead>
<tr>
<th align="center"></th>
<th align="center">public</th>
<th align="center">protected</th>
<th align="center">(default)</th>
<th align="center">private</th>
</tr>
</thead>
<tbody><tr>
<td align="center">同一个类（我自己）</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
</tr>
<tr>
<td align="center">同一个包（我邻居）</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
<td align="center">No</td>
</tr>
<tr>
<td align="center">不同包子类（我儿子）</td>
<td align="center">Yes</td>
<td align="center">Yes</td>
<td align="center">No</td>
<td align="center">No</td>
</tr>
<tr>
<td align="center">不同包非子类（陌生人）</td>
<td align="center">Yes</td>
<td align="center">No</td>
<td align="center">No</td>
<td align="center">No</td>
</tr>
</tbody></table>
<p>注意事项：</p>
<p>(default)不是关键字，而是根本不写</p>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>